package com.buyantech.smweb.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javacommon.base.BaseSpringController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.servlet.ModelAndView;

import cn.org.rapid_framework.page.Page;

import com.buyantech.smweb.excel.ExcelAVieData;
import com.buyantech.smweb.model.CreditsExchange;
import com.buyantech.smweb.model.Setting;
import com.buyantech.smweb.model.User;
import com.buyantech.smweb.service.BlackManager;
import com.buyantech.smweb.service.CreditsExchangeManager;
import com.buyantech.smweb.service.SettingManager;
import com.buyantech.smweb.service.UserManager;
import com.buyantech.smweb.vo.query.UserQuery;
import com.google.gson.Gson;

/**
 * 用户管理
 * 
 * @author Albert email:albert(a)buyantech.com
 * @version 1.0
 * @date 2012-05-10
 */
@Controller
public class UserController extends BaseSpringController {
	private UserManager userManager;
	private SettingManager settingManager;
	private CreditsExchangeManager creditsExchangeManager;

	private final String LIST_ACTION = "redirect:/pages/User/list.do";

	public UserController() {
	}

	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}

	public void setSettingManager(SettingManager settingManager) {
		this.settingManager = settingManager;
	}

	public void setCreditsExchangeManager(
			CreditsExchangeManager creditsExchangeManager) {
		this.creditsExchangeManager = creditsExchangeManager;
	}

	public void init(ModelMap model) {
		model.put("now", new java.sql.Timestamp(System.currentTimeMillis()));
	}

	/**
	 * 登录
	 **/
	public ModelAndView login(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String json = request.getParameter("submitData");
		Gson gson = new Gson();
		Map<String, String> map = new HashMap<String, String>();
		map = gson.fromJson(json, Map.class);
		List<User> list = userManager.login(map.get("userName"),
				map.get("password"));
		if (list != null && list.size() > 0) {
			request.getSession().setAttribute("user", list.get(0));
			ajaxPack("{state:true}", request, response);
		} else {
			ajaxPack("{state:false}", request, response);
		}
		return null;
	}

	/**
	 * 登录
	 **/
	public ModelAndView login2(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		User user = (User) request.getSession().getAttribute("user");
		ModelAndView result = new ModelAndView("/login");
		if (user != null) {
			result = new ModelAndView("/User/index");
		}
		return result;
	}

	/**
	 * 执行搜索
	 **/
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response, UserQuery query) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		query.setPageNumber(Integer.parseInt(request.getParameter("pageIndex")) + 1);
		query.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
		String sortfile = request.getParameter("sortField");
		if (sortfile != null && !sortfile.equals(""))
			query.setSortColumns(request.getParameter("sortField") + " "
					+ request.getParameter("sortOrder"));
		else
			query.setSortColumns(" register desc");
		Page page = this.userManager.searchPage(query);
		List<User> list = page.getResult();
		if (list != null) {
			User reuser = null;
			for (User user : list) {
				if (StringUtils.isNotEmpty(user.getRegister())) {
					user.setRegister(user.getRegister().substring(2));
				}
				if (user.getUsername() != null
						&& user.getUsername().equals("admin")) {
					reuser = user;
				}
			}
			list.remove(reuser);
		}
		modelMap.put("data", page.getResult());
		modelMap.put("total", page.getTotalCount());
		JSONObject obj = new JSONObject(modelMap);
		ajaxPack(obj.toString(), request, response);
		return null;
	}

	/**
	 * vip用户
	 **/
	public ModelAndView viplist(HttpServletRequest request,
			HttpServletResponse response, UserQuery query) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		query.setPageNumber(Integer.parseInt(request.getParameter("pageIndex")) + 1);
		query.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
		String sortfile = request.getParameter("sortField");
		if (sortfile != null && !sortfile.equals(""))
			query.setSortColumns(request.getParameter("sortField") + " "
					+ request.getParameter("sortOrder"));
		else
			query.setSortColumns(" register desc");
		Page page = this.userManager.searchPage1(query);
		List<User> list = page.getResult();
		if (list != null) {
			User reuser = null;
			for (User user : list) {
				if (StringUtils.isNotEmpty(user.getRegister())) {
					user.setRegister(user.getRegister().substring(2));
				}
				if (user.getUsername() != null
						&& user.getUsername().equals("admin")) {
					reuser = user;
				}
			}
			list.remove(reuser);
		}
		modelMap.put("data", page.getResult());
		modelMap.put("total", page.getTotalCount());
		JSONObject obj = new JSONObject(modelMap);
		ajaxPack(obj.toString(), request, response);
		return null;
	}

	/**
	 * 执行export
	 **/
	public ModelAndView exportData(HttpServletRequest request,
			HttpServletResponse response, UserQuery query) {
		ModelAndView result = new ModelAndView(new ExcelAVieData());
		String type = "";
		List<String> headers = new ArrayList<String>();
		headers.add("用户ID");
		headers.add("用户昵称");
		headers.add("邮箱");
		headers.add("手机号");
		headers.add("总积分");
		headers.add("兑换积分");
		headers.add("剩余积分");
		headers.add("注册时间");
		headers.add("IP");
		headers.add("版本号");
		headers.add("支付宝");
		headers.add("财付通");
		headers.add("QB");
		headers.add("话费");
		if (request.getParameter("paterid") != null
				&& !request.getParameter("paterid").equals(""))
			query.setPaterid(request.getParameter("paterid"));
		query.setPageSize(10000);
		Page page = this.userManager.searchPage(query);
		List<User> list = page.getResult();
//		if (list != null) {
//			for (User user : list) {
//				setAccounts(user);
//			}
//		}
		setAccounts(list);
		result.addObject("headers", headers);
		result.addObject("type", "2");
		result.addObject("content", page.getResult());
		response.setContentType("application/vnd.ms-excel");
		response.setHeader("Content-Desposition",
				"attachment; filename=demo.xls");
		return result;
	}

	/**
	 * 执行搜索
	 **/
	public ModelAndView xialist(HttpServletRequest request,
			HttpServletResponse response, UserQuery query) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		query.setPageNumber(Integer.parseInt(request.getParameter("pageIndex")) + 1);
		query.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
		String sortfile = request.getParameter("sortField");
		if (sortfile != null && !sortfile.equals(""))
			query.setSortColumns(request.getParameter("sortField") + " "
					+ request.getParameter("sortOrder"));
		else
			query.setSortColumns(" register desc");
		String account = request.getParameter("accountStr");
		Page page = this.userManager.searchPage(query);
		List<User> list = page.getResult();
		if (list != null) {
			User reuser = null;

			for (User user : list) {
				if (StringUtils.isNotEmpty(user.getPaterid())) {
					String uerId = userManager.getById(user.getPaterid())
							.getUserid();
					user.setPaterid(uerId);
				}
				if (StringUtils.isNotEmpty(user.getRegister())) {
					user.setRegister(user.getRegister().substring(2));
				}
				if (user.getUsername() != null
						&& user.getUsername().equals("admin")) {
					reuser = user;
				} 
					
			}

			list.remove(reuser);
			setAccounts(list);
			if (account != null && !account.equals("")) {
				Iterator<User> it = list.iterator();
				while (it.hasNext()) {
					User user = it.next();
					List list3 = creditsExchangeManager
							.getHql("from CreditsExchange where  account ='"
									+ account + "' and userid='"
									+ user.getUserid() + "'");
					if (list3 == null || list3.isEmpty()) {
						it.remove();

					}
				}
			}

		}
		modelMap.put("data", page.getResult());
		modelMap.put("total", page.getTotalCount());
		JSONObject obj = new JSONObject(modelMap);
		ajaxPack(obj.toString(), request, response);
		return null;
	}

	/**
	 * 登出
	 **/
	public ModelAndView delete(HttpServletRequest request,
			HttpServletResponse response, User user) throws Exception {
		String id = request.getParameter("id");
		if (id.length() > 0) {
			id = id.substring(0, id.length() - 1);
		}
		try {
			userManager.remove("DELETE FROM User WHERE id in(" + id + ")");
			ajaxPack("{state:true}", request, response);
		} catch (RuntimeException e) {
			ajaxPack("{state:false}", request, response);
		}
		return null;
	}

	/**
	 * 根据id获取用户对象
	 **/
	public ModelAndView getId(HttpServletRequest request,
			HttpServletResponse response, User user) throws Exception {
		String id = request.getParameter("id");
		user = userManager.getById(id);
		if (user != null) {
			setAccount(user);
			JSONObject obj = new JSONObject(user);
			ajaxPack(obj.toString(), request, response);
		}
		return null;
	}

	/**
	 * 保存新增对象
	 **/
	public ModelAndView save(HttpServletRequest request,
			HttpServletResponse response, User user) throws Exception {
		String daTypeStr = request.getParameter("submitData");
		Gson gson = new Gson();
		user = gson.fromJson(daTypeStr, User.class);
		try {
			if (StringUtils.isNotEmpty(user.getId()))
				userManager.update(user);
			else
				userManager.save(user);
		} catch (RuntimeException e) {
			ajaxPack("{state:false}", request, response);
			return null;
		}
		ajaxPack("{state:true}", request, response);
		return null;
	}

	/**
	 * 版本更新
	 **/
	public ModelAndView versions(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Setting setting = settingManager.getType("versions");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("versions");
			setting.setText("1");
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			String versions = setting.getText();
			int ver = Integer.parseInt(versions) + 1;
			setting.setText(ver + "");
			settingManager.update(setting);
		}
		ajaxPack("{state:true}", request, response);
		return null;
	}

	/**
	 * 上下级分配比例
	 **/
	public ModelAndView ratio(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String number = request.getParameter("number");
		Setting setting = settingManager.getType("ratio");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("ratio");
			setting.setText(number);
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			setting.setText(number);
			settingManager.update(setting);
		}
		settingManager.ratio=Integer.parseInt(number);
		ajaxPack("{state:true}", request, response);
		return null;
	}

	/**
	 * 间隔时间配置
	 **/
	public ModelAndView interval(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String interval = request.getParameter("interval");
		Setting setting = settingManager.getType("interval");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("interval");
			setting.setText(interval);
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			setting.setText(interval);
			settingManager.update(setting);
		}
		settingManager.interval=interval;
		ajaxPack("{state:true}", request, response);
		return null;
	}

	/**
	 * 积分配置
	 **/
	public ModelAndView integral(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String integral = request.getParameter("integral");
		Setting setting = settingManager.getType("integral");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("integral");
			setting.setText(integral);
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			setting.setText(integral);
			settingManager.update(setting);
		}
		settingManager.integral=Integer.parseInt(integral);
		ajaxPack("{state:true}", request, response);
		return null;
	}

	/**
	 * 兑换间隔时间配置
	 **/
	public ModelAndView exchange(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String exchange = request.getParameter("exchange");
		Setting setting = settingManager.getType("exchange");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("exchange");
			setting.setText(exchange);
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			setting.setText(exchange);
			settingManager.update(setting);
		}
		settingManager.exchange=Double.parseDouble(exchange);
		ajaxPack("{state:true}", request, response);
		return null;
	}

	/**
	 * AA:BB:CC最大允许的用户数配置
	 **/
	public ModelAndView usernum1(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String usernum1 = request.getParameter("usernum1");
		Setting setting = settingManager.getType("usernum1");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("usernum1");
			setting.setText(usernum1);
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			setting.setText(usernum1);
			settingManager.update(setting);
		}
		settingManager.usernum1=Integer.parseInt(usernum1);
		ajaxPack("{state:true}", request, response);
		return null;
	}

	/**
	 * AA:BB最大允许的用户数配置
	 **/
	public ModelAndView usernum2(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String usernum2 = request.getParameter("usernum2");
		Setting setting = settingManager.getType("usernum2");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("usernum2");
			setting.setText(usernum2);
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			setting.setText(usernum2);
			settingManager.update(setting);
		}
		settingManager.usernum2=Integer.parseInt(usernum2);
		ajaxPack("{state:true}", request, response);
		return null;
	}

	/**
	 * vip用户奖励积分配置
	 **/
	public ModelAndView vipnum(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String vipnum = request.getParameter("vipnum");
		Setting setting = settingManager.getType("vipnum");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("vipnum");
			setting.setText(vipnum);
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			setting.setText(vipnum);
			settingManager.update(setting);
		}
		settingManager.vipnum=Integer.parseInt(vipnum);
		ajaxPack("{state:true}", request, response);
		return null;
	}

	/**
	 * 同一个区域获取的相同积分个数最大配置
	 **/
	public ModelAndView num(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String num = request.getParameter("num");
		Setting setting = settingManager.getType("num");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("num");
			setting.setText(num);
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			setting.setText(num);
			settingManager.update(setting);
		}
		settingManager.num=Integer.parseInt(num);
		ajaxPack("{state:true}", request, response);
		return null;
	}

	public ModelAndView QBnumD(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String QBnumD = request.getParameter("QBnumD");
		Setting setting = settingManager.getType("QBnumD");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("QBnumD");
			setting.setText(QBnumD);
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			setting.setText(QBnumD);
			settingManager.update(setting);
		}
		settingManager.QBnumD=Integer.parseInt(QBnumD);
		ajaxPack("{state:true}", request, response);
		return null;
	}

	public ModelAndView QBnumM(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String QBnumM = request.getParameter("QBnumM");
		Setting setting = settingManager.getType("QBnumM");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("QBnumM");
			setting.setText(QBnumM);
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			setting.setText(QBnumM);
			settingManager.update(setting);
		}
		settingManager.QBnumM=Integer.parseInt(QBnumM);
		ajaxPack("{state:true}", request, response);
		return null;
	}

	public ModelAndView HFnumD(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String HFnumD = request.getParameter("HFnumD");
		Setting setting = settingManager.getType("HFnumD");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("HFnumD");
			setting.setText(HFnumD);
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			setting.setText(HFnumD);
			settingManager.update(setting);
		}
		settingManager.HFnumD=Integer.parseInt(HFnumD);
		ajaxPack("{state:true}", request, response);
		return null;
	}

	public ModelAndView HFnumM(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String HFnumM = request.getParameter("HFnumM");
		Setting setting = settingManager.getType("HFnumM");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("HFnumM");
			setting.setText(HFnumM);
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			setting.setText(HFnumM);
			settingManager.update(setting);
		}
		settingManager.HFnumM=Integer.parseInt(HFnumM);
		ajaxPack("{state:true}", request, response);
		return null;
	}

	/**
	 * 同一IP的最大用户数配置
	 **/
	public ModelAndView usernum(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String usernum = request.getParameter("usernum");
		Setting setting = settingManager.getType("usernum");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("usernum");
			setting.setText(usernum);
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			setting.setText(usernum);
			settingManager.update(setting);
		}
		settingManager.usernum=Integer.parseInt(usernum);
		ajaxPack("{state:true}", request, response);
		return null;
	}

	/**
	 * 24小时内同一渠道相同积分的个数最大数配置
	 **/
	public ModelAndView numMax(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String numMax = request.getParameter("numMax");
		Setting setting = settingManager.getType("numMax");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("numMax");
			setting.setText(numMax);
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			setting.setText(numMax);
			settingManager.update(setting);
		}
		settingManager.numMax=Integer.parseInt(numMax);
		ajaxPack("{state:true}", request, response);
		return null;
	}

	/**
	 * 24小时内同一渠道相同积分的个数最大数配置
	 **/
	public ModelAndView pushInterval(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String pushInterval = request.getParameter("pushInterval");
		Setting setting = settingManager.getType("pushInterval");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("pushInterval");
			setting.setText(pushInterval);
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			setting.setText(pushInterval);
			settingManager.update(setting);
		}
		SettingManager.pushInterval=Integer.parseInt(pushInterval);
		ajaxPack("{state:true}", request, response);
		return null;
	}

	/**
	 * 自动充值配置
	 **/
	public ModelAndView recharge(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String recharge = request.getParameter("recharge");
		Setting setting = settingManager.getType("recharge");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("recharge");
			setting.setText(recharge);
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			setting.setText(recharge);
			settingManager.update(setting);
		}
		SettingManager.recharge=recharge;
		ajaxPack("{state:true}", request, response);
		return null;
	}
	
	/**
	 * 版本参数数配置
	 **/
	public ModelAndView updateVersions(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String isf = request.getParameter("isf");
		String YesNo = request.getParameter("YesNo");
		Setting setting = settingManager.getType("isf");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("isf");
			setting.setText(YesNo);
			setting.setRemark(isf);
			settingManager.save(setting);
		} else {
			setting.setText(YesNo);
			setting.setRemark(isf);
			settingManager.update(setting);
		}
		ajaxPack("{state:true}", request, response);
		return null;
	}

	/**
	 * 更新内容提交接口
	 **/
	public ModelAndView updateUpdateInfo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		String json = request.getParameter("submitData");
		Gson gson = new Gson();
		Map<String, String> map = new HashMap<String, String>();
		map = gson.fromJson(json, Map.class);
		String updateInfo = (String) map.get("updateInfo");
		// updateInfo=new
		// String(updateInfo.getBytes("ISO-8859-1"),"UTF-8");//???
		Setting setting = settingManager.getType("updateInfo");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("updateInfo");
			setting.setText(updateInfo);
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			setting.setText(updateInfo);
			settingManager.update(setting);
		}
		settingManager.updateInfo=updateInfo;
		ajaxPack("{state:true}", request, response);
		return null;
	}

	/**
	 * 帮助提交接口
	 **/
	public ModelAndView setYesNo(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Setting setting = settingManager.getType("YesNo");
		if (setting == null) {
			setting = new Setting();
			setting.setTypes("YesNo");
			setting.setText("no");
			setting.setRemark("");
			settingManager.save(setting);
		} else {
			if (setting.getText().equals("yes")) {
				setting.setText("no");
			} else {
				setting.setText("yes");
			}
			settingManager.update(setting);
		}
		ajaxPack("{state:true}", request, response);
		return null;
	}

	/**
	 * 上下级分配比例
	 **/
	public ModelAndView settingList(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		try {
			Setting setting = settingManager.getType("ratio");
			Setting setting2 = settingManager.getType("versions");
			Setting setting3 = settingManager.getType("YesNo");
			Setting setting4 = settingManager.getType("interval");
			Setting setting5 = settingManager.getType("integral");
			Setting setting6 = settingManager.getType("exchange");
			Setting setting7 = settingManager.getType("usernum");
			Setting setting8 = settingManager.getType("isf");
			Setting setting9 = settingManager.getType("updateInfo");
			Setting setting10 = settingManager.getType("usernum1");
			Setting setting11 = settingManager.getType("usernum2");
			Setting setting12 = settingManager.getType("vipnum");
			Setting setting13 = settingManager.getType("num");
			Setting setting14 = settingManager.getType("QBnumD");
			Setting setting15 = settingManager.getType("QBnumM");
			Setting setting16 = settingManager.getType("HFnumD");
			Setting setting17 = settingManager.getType("HFnumM");
			Setting setting18 = settingManager.getType("numMax");
			Setting setting19 = settingManager.getType("pushInterval");
			Setting setting20 = settingManager.getType("recharge");
			if (setting != null) {
				modelMap.put("ratio", setting.getText());
			} else {
				modelMap.put("ratio", "");
			}
			if (setting2 != null) {
				modelMap.put("versions", setting2.getText());
			} else {
				modelMap.put("versions", "1");
			}
			if (setting3 != null) {
				modelMap.put("YesNo", setting3.getText());
			} else {
				modelMap.put("YesNo", "no");
			}
			if (setting4 != null) {
				modelMap.put("interval", setting4.getText());
			} else {
				modelMap.put("interval", "");
			}
			if (setting5 != null) {
				modelMap.put("integral", setting5.getText());
			} else {
				modelMap.put("integral", "");
			}
			if (setting6 != null) {
				modelMap.put("exchange", setting6.getText());
			} else {
				modelMap.put("exchange", "");
			}
			if (setting7 != null) {
				modelMap.put("usernum", setting7.getText());
			} else {
				modelMap.put("usernum", "");
			}
			if (setting8 != null) {
				modelMap.put("YesNo", setting8.getText());
				modelMap.put("isf", setting8.getRemark());
			} else {
				modelMap.put("YesNo", "");
				modelMap.put("isf", "");
			}
			if (setting9 != null) {
				modelMap.put("updateInfo", setting9.getText());
			} else {
				modelMap.put("updateInfo", "");
			}
			if (setting10 != null) {
				modelMap.put("usernum1", setting10.getText());
			} else {
				modelMap.put("usernum1", "");
			}
			if (setting11 != null) {
				modelMap.put("usernum2", setting11.getText());
			} else {
				modelMap.put("usernum2", "");
			}
			if (setting12 != null) {
				modelMap.put("vipnum", setting12.getText());
			} else {
				modelMap.put("vipnum", "");
			}
			if (setting13 != null) {
				modelMap.put("num", setting13.getText());
			} else {
				modelMap.put("num", "");
			}
			if (setting14 != null) {
				modelMap.put("QBnumD", setting14.getText());
			} else {
				modelMap.put("QBnumD", "");
			}
			if (setting15 != null) {
				modelMap.put("QBnumM", setting15.getText());
			} else {
				modelMap.put("QBnumM", "");
			}
			if (setting16 != null) {
				modelMap.put("HFnumD", setting16.getText());
			} else {
				modelMap.put("HFnumD", "");
			}
			if (setting17 != null) {
				modelMap.put("HFnumM", setting17.getText());
			} else {
				modelMap.put("HFnumM", "");
			}
			if (setting18 != null) {
				modelMap.put("numMax", setting18.getText());
			} else {
				modelMap.put("numMax", "");
			}
			if (setting19 != null) {
				modelMap.put("pushInterval", setting19.getText());
			} else {
				modelMap.put("pushInterval", "");
			}
			if (setting20 != null) {
				modelMap.put("recharge", setting20.getText());
			} else {
				modelMap.put("recharge", "");
			}
			modelMap.put("state", "true");
		} catch (Exception e) {
			modelMap.put("state", "false");
		}
		JSONObject obj = new JSONObject(modelMap);
		ajaxPack(obj.toString(), request, response);
		return null;
	}

	/**
	 * 登出
	 **/
	public ModelAndView resetpwd(HttpServletRequest request,
			HttpServletResponse response, User user) {
		String json = request.getParameter("submitData");
		Gson gson = new Gson();
		Map<String, String> map = new HashMap<String, String>();
		map = gson.fromJson(json, Map.class);
		User user2 = (User) request.getSession().getAttribute("user");
		if (user2 == null) {
			ajaxPack("{state:1}", request, response);
			return null;
		}
		if (!user2.getPasswrod().equals(map.get("lopwd"))) {
			ajaxPack("{state:2}", request, response);
			return null;
		}
		if (!map.get("newpwd").equals(map.get("fupwd"))) {
			ajaxPack("{state:3}", request, response);
			return null;
		}
		user2.setPasswrod(map.get("newpwd"));
		userManager.update(user2);
		ajaxPack("{success:4}", request, response);
		return null;
	}

	/**
	 * 更新黑名单
	 **/
	public ModelAndView updateIsvip(HttpServletRequest request,
			HttpServletResponse response, CreditsExchange creditsExchange)
			throws Exception {
		// SimpleDateFormat dateFm = new
		// SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			// String id = request.getParameter("id");
			// String json = request.getParameter("submitData");
			// Gson gson = new Gson();
			// Map<String, String> map = new HashMap<String, String>();
			// map = gson.fromJson(json, Map.class);
			// String remark = (String) map.get("remark");
			//
			// creditsExchange = creditsExchangeManager.getById(id);
			String userid = request.getParameter("userid");
			List<User> list = userManager.getHql(" from User where userid='"
					+ userid + "'");
			if (list != null && !list.isEmpty() && list.get(0) != null) {
				User mUser = list.get(0);
				mUser.setIsvip(1);
				userManager.update(mUser);
			} else {
				ajaxPack("{state:false}", request, response);
				return null;
			}
			// if (creditsExchange.getState().equals("1")) {
			// List<User> userlist = userManager
			// .getHql(" from User where userid='"
			// + creditsExchange.getUserid() + "'");
			// User user = userlist.get(0);
			// user.setHuIntegral(user.getHuIntegral()
			// - creditsExchange.getIntegral());
			// userManager.update(user);
			// creditsExchange.setState("3");
			// creditsExchange.setRemark(remark);
			// creditsExchange
			// .setOperTime(dateFm.format(new java.util.Date()));
			// creditsExchangeManager.update(creditsExchange);
			// }
		} catch (Exception e) {
			ajaxPack("{state:false}", request, response);
			return null;
		}
		ajaxPack("{state:true}", request, response);

		return null;
	}

	/**
	 * 删除黑名单
	 **/
	public ModelAndView updateIsvips(HttpServletRequest request,
			HttpServletResponse response, CreditsExchange creditsExchange)
			throws Exception {
		try {
			String userid = request.getParameter("userids");
			String[] userids = userid.split(",,,,,,,,,,");
			for (int i = 0; i < userids.length; i++) {
				List<User> list = userManager.getHql(" from User where userid="
						+ userids[i]);
				if (list != null && !list.isEmpty() && list.get(0) != null) {
					User mUser = list.get(0);
					mUser.setIsvip(0);
					userManager.update(mUser);
				}
			}
		} catch (Exception e) {
			ajaxPack("{state:false}", request, response);
			return null;
		}
		ajaxPack("{state:true}", request, response);

		return null;
	}

	private void setAccounts(List<User> list) {
		if (list != null&&list.size()>0) {
			StringBuffer str=new StringBuffer();
			str.append("(");
			for (User user : list) {
				str.append("'");
				str.append(user.getUserid());
				str.append("',");

			}
			str.deleteCharAt(str.length() - 1);
			str.append(")");
			List<CreditsExchange> list1 = creditsExchangeManager
					.getHql(" from CreditsExchange where userid in"+str.toString());
			if (list1 != null && !list1.isEmpty() && list1.get(0) != null) {

				for (CreditsExchange mCreditsExchange : list1) {
					for (User user : list) {

						if (mCreditsExchange.getUserid().equals(user.getUserid())) {
							if (mCreditsExchange.getTypeStr().equals("Q币")) {
								if (user.getQq1() == null)
									user.setQq1(mCreditsExchange.getAccount());
							} else if (mCreditsExchange.getTypeStr().equals("话费")) {
								if (user.getPhone1() == null)
									user.setPhone1(mCreditsExchange.getAccount());
							} else if (mCreditsExchange.getTypeStr().equals("财付通")) {
								if (user.getCft1() == null)
									user.setCft1(mCreditsExchange.getAccount());
							} else if (mCreditsExchange.getTypeStr().equals("支付宝")) {
								if (user.getZfb1() == null)
									user.setZfb1(mCreditsExchange.getAccount());
							}
						}
					}
				}
			}
			}
	}
	
	private void setAccount(User user) {
		List<CreditsExchange> list = creditsExchangeManager
				.getHql(" from CreditsExchange where  userid='"
						+ user.getUserid()
						+ "'");
		if (list != null && !list.isEmpty() && list.get(0) != null) {

			for (CreditsExchange mCreditsExchange : list) {

				if (mCreditsExchange.getTypeStr().equals("Q币")) {
					if (user.getQq1() == null)
						user.setQq1(mCreditsExchange.getAccount());
				} else if (mCreditsExchange.getTypeStr().equals("话费")) {
					if (user.getPhone1() == null)
						user.setPhone1(mCreditsExchange.getAccount());
				} else if (mCreditsExchange.getTypeStr().equals("财付通")) {
					if (user.getCft1() == null)
						user.setCft1(mCreditsExchange.getAccount());
				} else if (mCreditsExchange.getTypeStr().equals("支付宝")) {
					if (user.getZfb1() == null)
						user.setZfb1(mCreditsExchange.getAccount());
				}
			}
		}
	}
	
}
